package cn.edu.scau.cmi.shishuai.client;

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;

import java.util.ArrayList;
import java.util.List;

import cn.edu.scau.cmi.shishuai.Adapter.AdapterOfClass;
import cn.edu.scau.cmi.shishuai.Adapter.AdapterOfObject;
import cn.edu.scau.cmi.shishuai.Adapter.Student;

public class AdapterClient extends Application{

	private List<Student>studentList;
	private AdapterOfClass adapterOfClass=null;
	private AdapterOfObject adapterOfObject=null;
	private ObservableList<Person> data=null;
	
	@Override
	public void start(Stage primaryStage) throws Exception {
		// TODO Auto-generated method stub
		
		Label lable1 = new Label("选择模式");
		Label lable2 = new Label(" ");
		Button button1 = new Button("类适配器");
		Button button2 = new Button("对象适配器");
		Button addButton = new Button("添加");
		TableView<Person> table = new TableView();
		TableColumn idColumn=new TableColumn("学生ID");
		TableColumn nameColumn=new TableColumn("学生姓名");
		TextField idField = new TextField();
		TextField nameField = new TextField();
		
		lable1.setLayoutX(50);
		lable1.setLayoutY(53);
		lable2.setLayoutX(130);
		lable2.setLayoutY(53);
		button1.setLayoutX(250);
		button1.setLayoutY(50);
		button2.setLayoutX(350);
		button2.setLayoutY(50);
		table.setLayoutX(50);
		table.setLayoutY(150);
		idColumn.setMinWidth(200);
		idColumn.setCellValueFactory(
				new PropertyValueFactory<>("ID")
			);
		nameColumn.setMinWidth(200);
		nameColumn.setCellValueFactory(
				new PropertyValueFactory<>("name")
		);
		table.getColumns().addAll(idColumn,nameColumn);
		idField.setLayoutX(50);
		idField.setLayoutY(100);
		idField.setPrefWidth(150);
		idField.setPromptText("学生ID");
		nameField.setLayoutX(220);
		nameField.setLayoutY(100);
		nameField.setPrefWidth(150);
		nameField.setPromptText("学生姓名");
		addButton.setLayoutX(380);
		addButton.setLayoutY(100);

		button1.setOnAction((ActionEvent e)-> {
			studentList = new ArrayList<Student>();
			adapterOfClass = new AdapterOfClass();
			adapterOfObject = null;
			data = null;
			table.setItems(data);
			lable2.setText("类适配器模式");
		});
		
		button2.setOnAction((ActionEvent e)-> {
			studentList = new ArrayList<Student>();
			adapterOfClass = null;
			adapterOfObject = new AdapterOfObject();
			data = null;
			table.setItems(data);
			lable2.setText("对象适配器模式");
		});
		
		addButton.setOnAction((ActionEvent e)->{
			String ID = idField.getText(),name = nameField.getText();
			if(!ID.equals("")&&!name.equals("")&&(adapterOfClass!=null || adapterOfObject!=null)) {
				idField.clear();nameField.clear();
				int id = Integer.valueOf(ID);
				Student student = new Student(id,name);
				studentList.add(student);
				if(adapterOfClass!=null) {
					studentList = adapterOfClass.sortStudent(studentList);
				}
				else {
					studentList = adapterOfObject.sortStudent(studentList);
				}
				data = null;
				int siz = studentList.size();
				List<Person> personList = new ArrayList<Person>();
				for(Student student1:studentList) {
					Person person = new Person(String.valueOf(student1.getID()),student1.getName());
					personList.add(person);
				}
				
				data=FXCollections.observableArrayList(personList);
				table.setItems(data);
				
			}
		});
		
		Group group=new Group(lable1,lable2,button1,button2,table,idField,nameField,addButton);
		Scene scene=new Scene(group,600,600);
		primaryStage.setTitle("适配器模式");
		primaryStage.setScene(scene);
		primaryStage.show();
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Application.launch(args);
	}
	
	
	public static class Person {
		private final SimpleStringProperty ID;
		private final SimpleStringProperty name;
		private Person(String ID,String name) {
			this.ID=new SimpleStringProperty(ID);
			this.name=new SimpleStringProperty(name);
		}
		public String getID() {
			return ID.get();
		}
		public void setID(String name) {
			ID.set(name);
		}
		public String getName() {
			return name.get();
		}
		public void setName(String id) {
			name.set(id);
		}
	}
}